package com.yuexun.dhdoctor.module.commun.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.AppContext;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.baselistener.OnDataBackListener;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.Contract.bean.GroupGroupBean;
import com.yuexun.dhdoctor.module.Contract.bean.GroupGroupData;
import com.yuexun.dhdoctor.module.Contract.model.impl.GroupModel;
import com.yuexun.dhdoctor.module.Contract.presenter.impl.GroupPresenter;
import com.yuexun.dhdoctor.module.Contract.ui.view.IGroupView;
import com.yuexun.dhdoctor.module.commun.adapter.DepartAndGroup_Group_Adapter;
import com.yuexun.dhdoctor.module.commun.ui.Act_DepartAndGroup;
import com.yuexun.dhdoctor.module.commun.ui.Act_DepartAndGroup_Group;
import com.yuexun.dhdoctor.widget.EmptyLayout;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2017/3/27 0027.
 */

public class Frag_DepartAndGroup_Group extends KJFragment implements IGroupView {

    @Bind(R.id.group_lit)
    ExpandableListView groupLit;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private Handler handler;

    private GroupPresenter presenter;
    private List<GroupGroupData> publicgroupGroupDataBeans = new ArrayList<>();
    private List<GroupGroupData> groupGroupDataBeans = new ArrayList<>();
    private List<GroupGroupBean> groupGroupBeen = new ArrayList<>();
    //    private BaseAdapter adapter;
    private DepartAndGroup_Group_Adapter adapter;

    private ArrayList departmentBeans = new ArrayList<>();
    private OnDataBackListener listener;
    private String groupType = "2";
    private int isSelectMe;//0：不能选择  1：能选择自己

    public void setDepartment(ArrayList departmentBeans, OnDataBackListener listener, int isSelectMe) {
        this.departmentBeans = departmentBeans;
        this.listener = listener;
        this.isSelectMe = isSelectMe;
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_choice_group, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        super.initData();
        initHandle();
        initView();
    }

    @SuppressLint("HandlerLeak")
    void initHandle() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 10003:
                        if (listener != null) {
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("departmentBeans", departmentBeans);
                            listener.showData(bundle);
                        }
                        break;
                }
            }
        };
    }

    void initView() {
        groupLit.setGroupIndicator(null);
        groupLit.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        presenter = new GroupPresenter(new GroupModel(), this);

        adapter = new DepartAndGroup_Group_Adapter(AppContext.getInstance(), groupGroupBeen, departmentBeans, handler, isSelectMe);
        groupLit.setAdapter(adapter);

        groupLit.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent(AppContext.getInstance(), Act_DepartAndGroup_Group.class);
                bundle.putSerializable("DataBean", groupGroupBeen.get(i).getDatas().get(i1));
                intent.putExtras(bundle);
                getActivity().startActivityForResult(intent, 2);
                return true;
            }
        });

        groupGroupBeen.clear();
        groupType = "2";
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(AppContext.getInstance(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("groupType", groupType);
        presenter.getGroupDatas(params);

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);

            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            List depart = new ArrayList<>();
            depart.addAll(((Act_DepartAndGroup) getActivity()).departmentBeans);
            departmentBeans.clear();
            departmentBeans.addAll(depart);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showGroupDatas(BaseEntity<GroupGroupData> baseEntity) {
        if ("200".equals(baseEntity.getStatus())) {
            if (groupType.equals("2")) {
                if (publicgroupGroupDataBeans.size() > 0) {
                    publicgroupGroupDataBeans.clear();
                }
                publicgroupGroupDataBeans = baseEntity.getDatas();
                GroupGroupBean groupBean = new GroupGroupBean();
                groupBean.setGroup_name("公共群组");
                groupBean.setDatas(publicgroupGroupDataBeans);
                groupGroupBeen.add(groupBean);

                groupType = "1";
                Map<String, String> params = new HashMap<>();
                params.put("sessionUuid", PreferenceHelper.readString(AppContext.getInstance(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params.put("groupType", groupType);
                presenter.getGroupDatas(params);
            } else {
                if (groupGroupDataBeans.size() > 0) {
                    groupGroupDataBeans.clear();
                }
                groupGroupDataBeans = baseEntity.getDatas();

                GroupGroupBean groupBean = new GroupGroupBean();
                groupBean.setGroup_name("个人群组");
                groupBean.setDatas(groupGroupDataBeans);
                groupGroupBeen.add(groupBean);

                if (groupLit != null) {
                    adapter.setGroupGroupBeen(groupGroupBeen);
                    for (int i = 0; i < adapter.getGroupCount(); i++) {
                        groupLit.expandGroup(i);

                    }
                }
            }
        }

        if (emptyLayout != null) {
            emptyLayout.dismiss();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
